package xj.graph2d.app.prop;

import static xj.graph2d.app.resources.Resources.getResourceString;

import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;

public class TransparencyPanel extends JPanel {

  public TransparencyPanel() {
    setLayout(new BorderLayout());
    /*
    setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), 
					       getResourceString("Opacity.text"), 
					       TitledBorder.CENTER, TitledBorder.TOP,
					       titleFont, Color.darkGray));
    */
    setBorder(BorderFactory.createEtchedBorder());     
    JLabel title = new JLabel(getResourceString("Opacity.text"));
    title.setHorizontalAlignment(SwingConstants.CENTER);
    add(title, BorderLayout.NORTH); 

    JPanel panel = new JPanel();
    slider = new JSlider(SwingConstants.HORIZONTAL, 0, 100, 100);
    //slider.setMajorTickSpacing(50);
    //slider.setMinorTickSpacing(10);
    ////slider.setMinorTickSpacing(20);
    //slider.setPaintTicks(true);
    //slider.setPaintLabels(true);    

    //add(new JLabel("Transparency "));
    panel.add(new JLabel("0%"));
    panel.add(slider);
    panel.add(new JLabel("100%"));
    add(panel, BorderLayout.CENTER);
  }

  public void setValue(int v) {
    if (v < 0) {
      v = 0;
    } else if (v > 255) {
      v = 255;
    }
    float f = v / 2.55f;
    slider.setValue((int) f);
  }

  public int getValue() {
    int v = slider.getValue();
    return (int) (v * 2.55f);
  }

  public void addChangeListener(ChangeListener l) {
    slider.addChangeListener(l);
  }

  protected JSlider slider;

}
